<?php

namespace App\Models\Tool;

use App\Models\BaseModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class BadWord extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'word',
    ];

    protected $primaryKey = 'id';
    protected $dateFormat = 'U';
    protected $table = 'bad_words';

    /**
     * 检查屏蔽词
     * @param $val
     * @return int
     */
    public function checkBadWord($val){
        $bad_word = Cache::remember('bad_words', Carbon::now()->addMinutes(30), function () {
            return DB::table('bad_words')->get();
        });
        foreach($bad_word as $v){
            if(strpos($val, $v->word) !== false){
                return 40005;
            }
        }
        return 200;
    }
}
